﻿using System;

namespace Microsoft.Popfly.GameCreator.GameEngine.Events
{
    public class StandardListener : EventListener
    {
        public IEventChecker EventChecker { get; private set; }

        public void Update(double elapsedTimeInSeconds)
        {
            if (!QueuedForRemoval)
            {
                PGCEventArgs args;
                BehaviorOwner sender;
                if (EventChecker.FireThisFrame(elapsedTimeInSeconds, out sender, out args))
                {
                    if (Filter == null || Filter.CheckFilter())
                    {
                        if (AudioUrl != null)
                        {
                            m_game.PlayAudio(AudioUrl);
                        }
                        Behavior.Invoke(sender, args);
                    }
                }
            }
        }

        public StandardListener(
            Game game, 
            BehaviorOwner owner, 
            Filter filter, 
            Behaviors.IBehavior behavior, 
            String behaviorName, 
            String audioUrl,
            IEventChecker eventChecker) : base(game, owner, filter, behavior, behaviorName, audioUrl)
        {
            EventChecker = eventChecker;

        }

        public StandardListener(Game game, BehaviorOwner owner, Data.BehaviorInfo behaviorInfo)
            : base(game, owner, behaviorInfo)
        {
            EventChecker = EventCheckerFactory.CreateEventChecker(m_game, owner, this, behaviorInfo.Event);
        }
    }
}
